﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_BLL;
using Nova_MOD;
using Nova_Common;

public partial class System_Role_RoleFrame : System.Web.UI.Page
{
    Nova_BLL.Group Nova_BLL_Group = new Nova_BLL.Group();
    Nova_BLL.Menu Nova_BLL_Menu = new Nova_BLL.Menu();
   
    Nova_BLL.Role Nova_BLL_Role = new Nova_BLL.Role();
    Nova_MOD.Role Nova_MOD_Role = new Nova_MOD.Role();

    string strRole = "";  //权限的字符串
    int j = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlbind();
            gvbind();
            gvCheckbox();
        }
    }

    protected void gvbind()
    {
        gvMenu.DataSource = Nova_BLL_Menu.DataListMenu().Tables[0].DefaultView;
        gvMenu.DataBind();
    }

    protected void ddlbind()
    {
        ddlGroup.DataSource = Nova_BLL_Group.DataListGroup().Tables[0].DefaultView;
        ddlGroup.DataBind();

    }

    protected void gvCheckbox()
    {
        strRole = Nova_BLL_Role.ScalarRole(ddlGroup.SelectedValue.ToString());
        if (!string.IsNullOrEmpty(strRole))
        {
            string[] strRoleSplits = strRole.Split(',');
            foreach (string strRoleSplit in strRoleSplits)
            {
                if (gvMenu.Rows.Count > 0)
                {
                    for (int i = 0; i < gvMenu.Rows.Count; i++)
                    {
                        if (Convert.ToInt32(gvMenu.DataKeys[i].Value) == Convert.ToInt32(strRoleSplit))
                        {
                            ((CheckBox)gvMenu.Rows[i].Cells[0].FindControl("chkSelect")).Checked = true;
                        }
                    }
                }
            }
        }

    }

    protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvbind();
        gvCheckbox();
    }

    protected void ibtnSave_Click(object sender, ImageClickEventArgs e)
    {
        string strRoles = "";       //菜单ID, 绑字权限
        string IsFlag = "";                   //在tbGroupPermit表中，是否有对应选择的用户编号

        if (gvMenu.Rows.Count > 0)
        {
            for (int i = 0; i < gvMenu.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)gvMenu.Rows[i].Cells[0].FindControl("chkSelect");
                if (chk.Checked)
                {
                    strRoles += Convert.ToString(gvMenu.DataKeys[i].Value) + ",";
                }
            }
        }
        if (strRoles != "")
        {
            strRoles = strRoles.Substring(0, strRoles.Length - 1);
        }
        if (ddlGroup.SelectedValue != "")
        {
            IsFlag = Nova_BLL_Role.ScalarRole(ddlGroup.SelectedValue.ToString());

            Nova_MOD_Role.vGroupNo = ddlGroup.SelectedValue.ToString();
            Nova_MOD_Role.vPermit = strRoles.ToString();

            if (string.IsNullOrEmpty(IsFlag))
            {
                j = Nova_BLL_Role.AddRole(Nova_MOD_Role);
            }
            else
            {
                j = Nova_BLL_Role.UpdateRole(Nova_MOD_Role);
            }
            if (j > 0)
            {
                //Response.Write("<script language='javascript' type='text/javascript'>");
                //Response.Write("alert('保存成功！');");
                //Response.Write("</script>");
                Nova_Common.ShowBox.MessageBox(this, "保存成功！");

            }
            else
            {
                //Response.Write("<script language='javascript' type='text/javascript'>");
                //Response.Write("alert('没有发现组用户列表！');");
                //Response.Write("</script>");
                Nova_Common.ShowBox.MessageBox(this, "没有发现组用户列表！");
            }
        }
        else
        {
            Nova_Common.ShowBox.MessageBox(this, "没有选择工作组！");

        }
    }
    protected void gvMenu_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvMenu.PageIndex = e.NewPageIndex;
        gvbind();
        gvCheckbox();
    }
}

